using UnityEngine;
using UnityEngine.EventSystems;

public class DragDemo : MonoBehaviour, IPointerDownHandler, IDragHandler
{
    private RectTransform parentRTF;

    private void Start()
    {
        parentRTF = this.transform as RectTransform;
    }

    public void OnDrag(PointerEventData eventData)
    {
        RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRTF, eventData.position,
            eventData.pressEventCamera, out var worldPoint);
        this.transform.position = offset + worldPoint;
    }


    Vector3 offset = Vector3.zero;
    public void OnPointerDown(PointerEventData eventData)
    {
        RectTransformUtility.ScreenPointToWorldPointInRectangle(parentRTF, eventData.position,
            eventData.pressEventCamera, out var worldPoint);
        offset = this.transform.position - worldPoint;
    }
}
